// import { JSFuncs, Boards } from 'mixly'
import * as Blockly from 'blockly/core'

/**
 * Sentry3 MicroPython 生成器
 * 
 * 本文件提供将Blockly块转换为MicroPython代码的生成器函数。
 * 
 * 核心功能：
 * - cppToPythonEnum(): 将C++风格的枚举值映射为Python类成员
 *   例如："Sentry3::kVisionColor" -> "sentry3_vision_e.kVisionColor"
 * 
 * 使用方法：
 *   var cppValue = this.getFieldValue('vision_obj')  // 获取的是 "Sentry3::kVisionColor"
 *   var pythonValue = cppToPythonEnum(cppValue)       // 转换为 "sentry3_vision_e.kVisionColor"
 *   var code = `sentry3.VisionBegin(${pythonValue})` // 生成Python代码
 * 
 * 这样设计的好处：
 * 1. 可以在Mind+等环境中使用相同的block定义
 * 2. C++和Python生成器可以共用block配置
 * 3. 避免了枚举值不兼容的问题
 */

/**
 * C++枚举值到Python类成员的映射表
 */
const cppToPythonEnumMap = {
    // Vision types
    'Sentry3::kVisionColor': 'sentry3_vision_e.kVisionColor',
    'Sentry3::kVisionBlob': 'sentry3_vision_e.kVisionBlob',
    'Sentry3::kVisionAprilTag': 'sentry3_vision_e.kVisionAprilTag',
    'Sentry3::kVisionLine': 'sentry3_vision_e.kVisionLine',
    'Sentry3::kVisionLearning': 'sentry3_vision_e.kVisionLearning',
    'Sentry3::kVisionFace': 'sentry3_vision_e.kVisionFace',
    'Sentry3::kVisionYolo': 'sentry3_vision_e.kVisionYolo',
    'Sentry3::kVisionQrCode': 'sentry3_vision_e.kVisionQrCode',
    'Sentry3::kVisionBarCode': 'sentry3_vision_e.kVisionBarCode',
    'Sentry3::kVisionOCR': 'sentry3_vision_e.kVisionOCR',
    'Sentry3::kVisionHandPose': 'sentry3_vision_e.kVisionHandPose',
    'Sentry3::kVisionLicensePlate': 'sentry3_vision_e.kVisionLicensePlate',

    // Object info
    'kStatus': 'sentry_obj_info_e.kStatus',
    'kXValue': 'sentry_obj_info_e.kXValue',
    'kYValue': 'sentry_obj_info_e.kYValue',
    'kWidthValue': 'sentry_obj_info_e.kWidthValue',
    'kHeightValue': 'sentry_obj_info_e.kHeightValue',
    'kLabel': 'sentry_obj_info_e.kLabel',
    'kRValue': 'sentry_obj_info_e.kRValue',
    'kGValue': 'sentry_obj_info_e.kGValue',
    'kBValue': 'sentry_obj_info_e.kBValue',

    // Color labels
    'Sentry3::kColorBlack': 'color_label_e.kColorBlack',
    'Sentry3::kColorWhite': 'color_label_e.kColorWhite',
    'Sentry3::kColorRed': 'color_label_e.kColorRed',
    'Sentry3::kColorGreen': 'color_label_e.kColorGreen',
    'Sentry3::kColorBlue': 'color_label_e.kColorBlue',
    'Sentry3::kColorYellow': 'color_label_e.kColorYellow',

    // YOLO labels
    'Sentry3::kPerson': 'yolo_label_e.kPerson',
    'Sentry3::kBicycle': 'yolo_label_e.kBicycle',
    'Sentry3::kCar': 'yolo_label_e.kCar',
    'Sentry3::kMotorcycle': 'yolo_label_e.kMotorcycle',
    'Sentry3::kAirplane': 'yolo_label_e.kAirplane',
    'Sentry3::kBus': 'yolo_label_e.kBus',
    'Sentry3::kTrain': 'yolo_label_e.kTrain',
    'Sentry3::kTruck': 'yolo_label_e.kTruck',
    'Sentry3::kBoat': 'yolo_label_e.kBoat',
    'Sentry3::kTrafficLight': 'yolo_label_e.kTrafficLight',
    'Sentry3::kFireHydrant': 'yolo_label_e.kFireHydrant',
    'Sentry3::kStopSign': 'yolo_label_e.kStopSign',
    'Sentry3::kParkingMeter': 'yolo_label_e.kParkingMeter',
    'Sentry3::kBench': 'yolo_label_e.kBench',
    'Sentry3::kBird': 'yolo_label_e.kBird',
    'Sentry3::kCat': 'yolo_label_e.kCat',
    'Sentry3::kDog': 'yolo_label_e.kDog',
    'Sentry3::kHorse': 'yolo_label_e.kHorse',
    'Sentry3::kSheep': 'yolo_label_e.kSheep',
    'Sentry3::kCow': 'yolo_label_e.kCow',
    'Sentry3::kElephant': 'yolo_label_e.kElephant',
    'Sentry3::kBear': 'yolo_label_e.kBear',
    'Sentry3::kZebra': 'yolo_label_e.kZebra',
    'Sentry3::kGiraffe': 'yolo_label_e.kGiraffe',
    'Sentry3::kBackpack': 'yolo_label_e.kBackpack',
    'Sentry3::kUmbrella': 'yolo_label_e.kUmbrella',
    'Sentry3::kHandbag': 'yolo_label_e.kHandbag',
    'Sentry3::kTie': 'yolo_label_e.kTie',
    'Sentry3::kSuitcase': 'yolo_label_e.kSuitcase',
    'Sentry3::kFrisbee': 'yolo_label_e.kFrisbee',
    'Sentry3::kSkis': 'yolo_label_e.kSkis',
    'Sentry3::kSnowboard': 'yolo_label_e.kSnowboard',
    'Sentry3::kSportsBall': 'yolo_label_e.kSportsBall',
    'Sentry3::kKite': 'yolo_label_e.kKite',
    'Sentry3::kBaseballBat': 'yolo_label_e.kBaseballBat',
    'Sentry3::kBaseballGlove': 'yolo_label_e.kBaseballGlove',
    'Sentry3::kSkateboard': 'yolo_label_e.kSkateboard',
    'Sentry3::kSurfboard': 'yolo_label_e.kSurfboard',
    'Sentry3::kTennisRacket': 'yolo_label_e.kTennisRacket',
    'Sentry3::kBottle': 'yolo_label_e.kBottle',
    'Sentry3::kWineGlass': 'yolo_label_e.kWineGlass',
    'Sentry3::kCup': 'yolo_label_e.kCup',
    'Sentry3::kFork': 'yolo_label_e.kFork',
    'Sentry3::kKnife': 'yolo_label_e.kKnife',
    'Sentry3::kSpoon': 'yolo_label_e.kSpoon',
    'Sentry3::kBowl': 'yolo_label_e.kBowl',
    'Sentry3::kBanana': 'yolo_label_e.kBanana',
    'Sentry3::kApple': 'yolo_label_e.kApple',
    'Sentry3::kSandwich': 'yolo_label_e.kSandwich',
    'Sentry3::kOrange': 'yolo_label_e.kOrange',
    'Sentry3::kBroccoli': 'yolo_label_e.kBroccoli',
    'Sentry3::kCarrot': 'yolo_label_e.kCarrot',
    'Sentry3::kHotDog': 'yolo_label_e.kHotDog',
    'Sentry3::kPizza': 'yolo_label_e.kPizza',
    'Sentry3::kDonut': 'yolo_label_e.kDonut',
    'Sentry3::kCake': 'yolo_label_e.kCake',
    'Sentry3::kChair': 'yolo_label_e.kChair',
    'Sentry3::kCouch': 'yolo_label_e.kCouch',
    'Sentry3::kPottedPlant': 'yolo_label_e.kPottedPlant',
    'Sentry3::kBed': 'yolo_label_e.kBed',
    'Sentry3::kDiningTable': 'yolo_label_e.kDiningTable',
    'Sentry3::kToilet': 'yolo_label_e.kToilet',
    'Sentry3::kTv': 'yolo_label_e.kTv',
    'Sentry3::kLaptop': 'yolo_label_e.kLaptop',
    'Sentry3::kMouse': 'yolo_label_e.kMouse',
    'Sentry3::kRemote': 'yolo_label_e.kRemote',
    'Sentry3::kKeyboard': 'yolo_label_e.kKeyboard',
    'Sentry3::kCellPhone': 'yolo_label_e.kCellPhone',
    'Sentry3::kMicrowave': 'yolo_label_e.kMicrowave',
    'Sentry3::kOven': 'yolo_label_e.kOven',
    'Sentry3::kToaster': 'yolo_label_e.kToaster',
    'Sentry3::kSink': 'yolo_label_e.kSink',
    'Sentry3::kRefrigerator': 'yolo_label_e.kRefrigerator',
    'Sentry3::kBook': 'yolo_label_e.kBook',
    'Sentry3::kClock': 'yolo_label_e.kClock',
    'Sentry3::kVase': 'yolo_label_e.kVase',
    'Sentry3::kScissors': 'yolo_label_e.kScissors',
    'Sentry3::kTeddyBear': 'yolo_label_e.kTeddyBear',
    'Sentry3::kHairDrier': 'yolo_label_e.kHairDrier',
    'Sentry3::kToothbrush': 'yolo_label_e.kToothbrush',

    // AprilTag modes
    'Sentry3::kVisionModeFamily16H5': 'apriltag_vision_mode_e.kVisionModeFamily16H5',
    'Sentry3::kVisionModeFamily25H9': 'apriltag_vision_mode_e.kVisionModeFamily25H9',
    'Sentry3::kVisionModeFamily36H11': 'apriltag_vision_mode_e.kVisionModeFamily36H11',

    // LED colors
    'kLedClose': 'sentry_led_color_e.kLedClose',
    'kLedRed': 'sentry_led_color_e.kLedRed',
    'kLedGreen': 'sentry_led_color_e.kLedGreen',
    'kLedYellow': 'sentry_led_color_e.kLedYellow',
    'kLedBlue': 'sentry_led_color_e.kLedBlue',
    'kLedPurple': 'sentry_led_color_e.kLedPurple',
    'kLedCyan': 'sentry_led_color_e.kLedCyan',
    'kLedWhite': 'sentry_led_color_e.kLedWhite',
}

/**
 * 将C++枚举值转换为Python类成员
 * @param {string} cppValue - C++风格的枚举值，如 "Sentry3::kVisionColor"
 * @returns {string} Python风格的类成员，如 "sentry3_vision_e.kVisionColor"
 */
export function cppToPythonEnum(cppValue) {
    // 如果已经在映射表中，直接返回
    if (cppToPythonEnumMap[cppValue]) {
        return cppToPythonEnumMap[cppValue]
    }

    // 尝试处理嵌套命名空间，如 Sentry3::kVisionColor -> sentry3_vision_e.kVisionColor
    if (cppValue.includes('::')) {
        const parts = cppValue.split('::')
        const enumName = parts[parts.length - 1]
        
        // 尝试查找映射
        if (cppToPythonEnumMap[enumName]) {
            return cppToPythonEnumMap[enumName]
        }
        
        // 如果没有找到，尝试基于命名空间推断
        if (parts[0] === 'Sentry3') {
            const enumValue = enumName.replace(/^k/, '').toLowerCase() + '_e'
            return `${enumValue}.${enumName}`
        }
    }

    // 如果都没找到，返回原值（可能是已经是Python格式或者需要手动处理）
    return cppValue
}

// Sentry3
export const Sentry3Begin = function (_, generator) {
    generator = generator ?? Blockly.Python
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var mode = this.getFieldValue('mode_obj')
    var addr = this.getFieldValue('addr_obj')

    generator.definitions_['import_Sentry3'] = 'from Sentry3 import *'

    if (mode == 'uart2') {
        generator.definitions_['import_UART'] =
            'from machine import UART'
        generator.definitions_['init_UART'] =
            `${mode} = UART(2, baudrate=115200)`
    } else if (mode == 'softi2c') {
        generator.definitions_['import_IIC'] =
            'from machine import Pin,SoftI2C'
        generator.definitions_['init_IIC'] =
            `${mode} = SoftI2C(scl=Pin(13), sda=Pin(14), freq=400000)`
    } else {
        mode = 'i2c'
    }

    generator.definitions_[`init_Sentry3_${dropdown_sentry_index}`] = `sentry3_${dropdown_sentry_index}  = Sentry3(${addr} )`
    var code = `while sentry3_${dropdown_sentry_index}.begin(${mode}) != 0:\n    print("Sentry3 begin failed, retrying...")\n`
    return code
}

export const Sentry3VisionSetStatus = function (_, generator) {
    generator = generator ?? Blockly.Python
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var VisionStatus = this.getFieldValue('VisionStatus')
    var vision_obj = this.getFieldValue('vision_obj')
    // 使用映射函数将C++枚举转换为Python类成员
    var python_vision_obj = cppToPythonEnum(vision_obj)
    var code = `sentry3_${dropdown_sentry_index}.Vision${VisionStatus}(${python_vision_obj})\n`

    return code
}

export const Sentry3VisionSetParamNum = function (_, generator) {
    generator = generator ?? Blockly.Python
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var vision_obj = this.getFieldValue('vision_obj')
    // 使用映射函数转换
    var python_vision_obj = cppToPythonEnum(vision_obj)
    var max_num = generator.valueToCode(
        this,
        'num',
        generator.ORDER_NONE,
    )
    var code = `sentry3_${dropdown_sentry_index}.SetParamNum(${python_vision_obj}, ${max_num})\n`

    return code
}

export const Sentry3VisionColorSetParam = function (_, generator) {
    generator = generator ?? Blockly.Python
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var x =
        generator.valueToCode(this, 'x', generator.ORDER_NONE) ||
        '1'
    var y =
        generator.valueToCode(this, 'y', generator.ORDER_NONE) ||
        '1'
    var w =
        generator.valueToCode(this, 'w', generator.ORDER_NONE) ||
        '1'
    var h =
        generator.valueToCode(this, 'h', generator.ORDER_NONE) ||
        '1'
    var index = generator.valueToCode(
        this,
        'index',
        generator.ORDER_ATOMIC,
    )
    var code =
        `sentry3_${dropdown_sentry_index}` +
        '.SetParam(' +
        'sentry3_vision_e.kVisionColor, [' +
        x +
        ', ' +
        y +
        ', ' +
        w +
        ', ' +
        h +
        ', 0]' +
        ', ' +
        index +
        ')\n'

    return code
}

export const Sentry3VisionBlobSetParam = function (_, generator) {
    generator = generator ?? Blockly.Python
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var w =
        generator.valueToCode(this, 'w', generator.ORDER_NONE) ||
        '1'
    var h =
        generator.valueToCode(this, 'h', generator.ORDER_NONE) ||
        '1'
    var lable = this.getFieldValue('lable')
    // 使用映射函数转换
    var python_lable = cppToPythonEnum(lable)
    var index = generator.valueToCode(
        this,
        'index',
        generator.ORDER_ATOMIC,
    )
    var code =
        `sentry3_${dropdown_sentry_index}` +
        '.SetParam(' +
        'sentry3_vision_e.kVisionBlob, ' +
        '[0, 0, ' +
        w +
        ', ' +
        h +
        ', ' +
        python_lable +
        ']' +
        ', ' +
        index +
        ')\n'

    return code
}
export const Sentry3VisionSetParam = function (_, generator) {
    generator = generator ?? Blockly.Python
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var vision_obj = this.getFieldValue('vision_obj')
    // 使用映射函数转换
    var python_vision_obj = cppToPythonEnum(vision_obj)
    var lable = this.getFieldValue('lable')
    // 使用映射函数转换lable
    var python_lable = cppToPythonEnum(lable)
    var index = generator.valueToCode(
        this,
        'index',
        generator.ORDER_ATOMIC,
    )
    var code =
        `sentry3_${dropdown_sentry_index}` +
        '.SetParam(' +
        python_vision_obj +
        ', [0, 0, 0, 0,' +
        python_lable +
        ']' +
        ', ' +
        index +
        ')\n'

    return code
}
export const Sentry3VisionSetBlobNum = function (_, generator) {
    generator = generator ?? Blockly.Python
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    // 使用映射函数转换
    var python_vision_obj = cppToPythonEnum(dropdown_vision_obj)
    var input_mode = generator.valueToCode(
        this,
        'num',
        generator.ORDER_ATOMIC,
    )

    var code = `sentry3_${dropdown_sentry_index}.VisionSetMode(${python_vision_obj},${input_mode})\n`

    return code
}

export const Sentry3VisionSetAprilTagMode = function (_, generator) {
    generator = generator ?? Blockly.Python
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    var input_mode = this.getFieldValue('mode')
    // 使用映射函数转换vision类型和模式
    var python_vision_obj = cppToPythonEnum(dropdown_vision_obj)
    var python_mode = cppToPythonEnum(input_mode)

    var code = `sentry3_${dropdown_sentry_index}.VisionSetMode(${python_vision_obj},${python_mode})\n`

    return code
}

export const Sentry3VisionSetBarCodeMode = function (_, generator) {
    generator = generator ?? Blockly.Python
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    var input_mode = this.getFieldValue('mode')
    // 使用映射函数转换
    var python_vision_obj = cppToPythonEnum(dropdown_vision_obj)

    var code = `sentry3_${dropdown_sentry_index}.VisionSetMode(${python_vision_obj},${input_mode})\n`

    return code
}

export const Sentry3VisionSetLineNum = function (_, generator) {
    generator = generator ?? Blockly.Python
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    // 使用映射函数转换
    var python_vision_obj = cppToPythonEnum(dropdown_vision_obj)
    var input_mode = generator.valueToCode(
        this,
        'num',
        generator.ORDER_ATOMIC,
    )

    var code = `sentry3_${dropdown_sentry_index}.VisionSetMode(${python_vision_obj},${input_mode})\n`

    return code
}

export const Sentry3LedSetColor = function (_, generator) {
    generator = generator ?? Blockly.Python
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var color_dic = {
        '#000000': 'sentry_led_color_e.kLedClose',
        '#ff0000': 'sentry_led_color_e.kLedRed',
        '#00ff00': 'sentry_led_color_e.kLedGreen',
        '#ffff00': 'sentry_led_color_e.kLedYellow',
        '#0000ff': 'sentry_led_color_e.kLedBlue',
        '#ff00ff': 'sentry_led_color_e.kLedPurple',
        '#00ffff': 'sentry_led_color_e.kLedCyan',
        '#ffffff': 'sentry_led_color_e.kLedWhite',
    }

    var dropdown_led_color_obj1 =
        color_dic[this.getFieldValue('led_color_obj1')]
    var dropdown_led_color_obj2 =
        color_dic[this.getFieldValue('led_color_obj2')]

    var input_level =
        generator.valueToCode(
            this,
            'level',
            generator.ORDER_ATOMIC,
        ) || 1

    return `sentry3_${dropdown_sentry_index}.LedSetColor(${dropdown_led_color_obj1},${dropdown_led_color_obj2},${input_level})\n`
}

export const Sentry3VisionDetectedCount = function (_, generator) {
    generator = generator ?? Blockly.Python
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var vision_obj = this.getFieldValue('vision_obj')
    // 使用映射函数转换
    var python_vision_obj = cppToPythonEnum(vision_obj)
    var code =
        `sentry3_${dropdown_sentry_index}.GetValue(${python_vision_obj}, sentry_obj_info_e.kStatus)`

    return [code, generator.ORDER_ATOMIC]
}


export const Sentry3GetVisionObjValue = function (_, generator) {
    generator = generator ?? Blockly.Python
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var vision_obj = this.getFieldValue('vision_obj')
    var vision_res_obj = this.getFieldValue('vision_res_obj')
    // 使用映射函数转换vision类型
    var python_vision_obj = cppToPythonEnum(vision_obj)
    var python_vision_res_obj = cppToPythonEnum(vision_res_obj)
    var index =
        generator.valueToCode(
            this,
            'index',
            generator.ORDER_NONE,
        ) || '1'
    var code =
        `sentry3_${dropdown_sentry_index}` +
        `.GetValue(${python_vision_obj}, ${python_vision_res_obj}, ${index})`

    return [code, generator.ORDER_ATOMIC]
}

export const Sentry3GetVisionObjLineValue = function (_, generator) {
    generator = generator ?? Blockly.Python
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    var dropdown_res_obj = this.getFieldValue('vision_res_obj')
    // 使用映射函数转换
    var python_vision_obj = cppToPythonEnum(dropdown_vision_obj)
    var python_res_obj = cppToPythonEnum(dropdown_res_obj)
    var input_index =
        generator.valueToCode(this, 'index', generator.ORDER_ATOMIC) || '0'

    var code = `sentry3_${dropdown_sentry_index}.GetValue(${python_vision_obj}, ${python_res_obj}, ${input_index})`
    return [code, generator.ORDER_ATOMIC]
}

export const Sentry3GetVisionObjStringValue = function (_, generator) {
    generator = generator ?? Blockly.Python
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    var dropdown_res_obj = this.getFieldValue('vision_res_obj')
    // 使用映射函数转换
    var python_vision_obj = cppToPythonEnum(dropdown_vision_obj)
    var python_res_obj = cppToPythonEnum(dropdown_res_obj)
    var input_index =
        generator.valueToCode(this, 'index', generator.ORDER_ATOMIC) || '0'

    var code = `sentry3_${dropdown_sentry_index}.GetValue(${python_vision_obj}, ${python_res_obj}, ${input_index})`
    return [code, generator.ORDER_ATOMIC]
}

export const Sentry3GetVisionObjColorValue = function (_, generator) {
    generator = generator ?? Blockly.Python
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    var dropdown_res_obj = this.getFieldValue('vision_res_obj')
    // 使用映射函数转换
    var python_vision_obj = cppToPythonEnum(dropdown_vision_obj)
    var python_res_obj = cppToPythonEnum(dropdown_res_obj)
    var input_index =
        generator.valueToCode(this, 'index', generator.ORDER_ATOMIC) || '0'

    var code = `sentry3_${dropdown_sentry_index}.GetValue(${python_vision_obj}, ${python_res_obj}, ${input_index})`
    return [code, generator.ORDER_ATOMIC]
}

export const Sentry3GetString = function (_, generator) {
    generator = generator ?? Blockly.Python
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    // 使用映射函数转换
    var python_vision_obj = cppToPythonEnum(dropdown_vision_obj)
    var input_index =
        generator.valueToCode(this, 'index', generator.ORDER_ATOMIC)
    var code = `str(sentry3_${dropdown_sentry_index}.GetString(${python_vision_obj}, ${input_index}))`
    return [code, generator.ORDER_ATOMIC]
}


// 检测颜色
export const Sentry3VisionDetectedColor = function (_, generator) {
    generator = generator ?? Blockly.Python
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    var dropdown_card_obj = this.getFieldValue('vision_card_obj')
    var input_index =
        generator.valueToCode(this, 'index', generator.ORDER_ATOMIC) || '0'

    // 使用映射函数转换
    var python_vision_obj = cppToPythonEnum(dropdown_vision_obj)
    var python_card_obj = cppToPythonEnum(dropdown_card_obj)
    
    var code = `(sentry3_${dropdown_sentry_index}.GetValue(${python_vision_obj}, sentry_obj_info_e.kLabel, ${input_index}) == ${python_card_obj})`
    return [code, generator.ORDER_ATOMIC]
}

// 检测色块
export const Sentry3VisionDetectedBlob = function (_, generator) {
    generator = generator ?? Blockly.Python
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    var dropdown_card_obj = this.getFieldValue('vision_card_obj')
    var input_index =
        generator.valueToCode(this, 'index', generator.ORDER_ATOMIC) || '0'

    // 使用映射函数转换
    var python_vision_obj = cppToPythonEnum(dropdown_vision_obj)
    var python_card_obj = cppToPythonEnum(dropdown_card_obj)
    
    var code = `(sentry3_${dropdown_sentry_index}.GetValue(${python_vision_obj}, sentry_obj_info_e.kLabel, ${input_index}) == ${python_card_obj})`
    return [code, generator.ORDER_ATOMIC]
}

// 检测YOLO物体
export const Sentry3VisionDetectedYolo = function (_, generator) {
    generator = generator ?? Blockly.Python
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    var dropdown_card_obj = this.getFieldValue('vision_card_obj')
    var input_index =
        generator.valueToCode(this, 'index', generator.ORDER_ATOMIC) || '0'

    // 使用映射函数转换
    var python_vision_obj = cppToPythonEnum(dropdown_vision_obj)
    var python_card_obj = cppToPythonEnum(dropdown_card_obj)
    
    var code = `(sentry3_${dropdown_sentry_index}.GetValue(${python_vision_obj}, sentry_obj_info_e.kLabel, ${input_index}) == ${python_card_obj})`
    return [code, generator.ORDER_ATOMIC]
}

// 检测手势 - 自定义
export const Sentry3VisionDetectedHandPoseCustom = function (_, generator) {
    generator = generator ?? Blockly.Python
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    var input_index =
        generator.valueToCode(this, 'index', generator.ORDER_ATOMIC) || '0'

    // 使用映射函数转换vision类型
    var python_vision_obj = cppToPythonEnum(dropdown_vision_obj)

    // 获取每个手指的状态：bent(弯曲=0), extended(张开=1), ignore(忽略=-1)
    var thumb_state = this.getFieldValue('thumb_state')
    var index_state = this.getFieldValue('index_state')
    var middle_state = this.getFieldValue('middle_state')
    var ring_state = this.getFieldValue('ring_state')
    var pinky_state = this.getFieldValue('pinky_state')

    // 计算mask和status
    // 位顺序：bit0(大拇指) bit1(食指) bit2(中指) bit3(无名指) bit4(小拇指)
    var mask = 0
    var status = 0

    // 大拇指 (bit 0)
    if (thumb_state !== 'ignore') {
        mask |= 0b00001
        if (thumb_state === 'extended') {
            status |= 0b00001
        }
    }

    // 食指 (bit 1)
    if (index_state !== 'ignore') {
        mask |= 0b00010
        if (index_state === 'extended') {
            status |= 0b00010
        }
    }

    // 中指 (bit 2)
    if (middle_state !== 'ignore') {
        mask |= 0b00100
        if (middle_state === 'extended') {
            status |= 0b00100
        }
    }

    // 无名指 (bit 3)
    if (ring_state !== 'ignore') {
        mask |= 0b01000
        if (ring_state === 'extended') {
            status |= 0b01000
        }
    }

    // 小拇指 (bit 4)
    if (pinky_state !== 'ignore') {
        mask |= 0b10000
        if (pinky_state === 'extended') {
            status |= 0b10000
        }
    }

    // 生成代码：((label & mask) == status)
    // 先获取label值
    var label_code = `sentry3_${dropdown_sentry_index}.GetValue(${python_vision_obj}, sentry_obj_info_e.kLabel, ${input_index})`
    // 将mask和status转换为5位二进制字符串
    var mask_bin = mask.toString(2)
    var mask_str = '0b' + ('00000' + mask_bin).slice(-5)
    var status_bin = status.toString(2)
    var status_str = '0b' + ('00000' + status_bin).slice(-5)
    var code = `((${label_code} & ${mask_str}) == ${status_str})`

    return [code, generator.ORDER_ATOMIC]
}

// 检测手势 - 预定义
export const Sentry3VisionDetectedHandPosePreset = function (_, generator) {
    generator = generator ?? Blockly.Python
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    var dropdown_handpose_preset = this.getFieldValue('handpose_preset')
    var input_index =
        generator.valueToCode(this, 'index', generator.ORDER_ATOMIC) || '0'

    // 使用映射函数转换vision类型
    var python_vision_obj = cppToPythonEnum(dropdown_vision_obj)

    // 去除前缀大写字母（如 A0b00010 -> 0b00010）
    var preset_value = dropdown_handpose_preset.replace(/^[A-Z]/, '')

    // 预定义手势的值已经是二进制格式（如 0b00010）
    // 生成代码：((label & 0b11111) == preset_value)
    var label_code = `sentry3_${dropdown_sentry_index}.GetValue(${python_vision_obj}, sentry_obj_info_e.kLabel, ${input_index})`
    var code = `((${label_code} & 0b11111) == ${preset_value})`

    return [code, generator.ORDER_ATOMIC]
}